package com.woniuxy.woniumall.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniumall.entity.Book;
import com.woniuxy.woniumall.service.BookService;
import com.woniuxy.woniumall.vo.BookVo;
import com.woniuxy.woniumall.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("book")
public class BookController {

    @Autowired
    BookService bookService ;

    @GetMapping("/searchBook")
    @ResponseBody
    public ResponseResult<PageInfo<BookVo>> searchBook(String name,
                          @RequestParam(value = "cataId",defaultValue = "0") Integer cataId,
                          @RequestParam(value = "now",defaultValue = "1")Integer now,
                          @RequestParam(value = "size",defaultValue = "3")Integer size){
        ResponseResult<PageInfo<BookVo>> result = new ResponseResult<>();
        try {
            PageInfo<BookVo> pageInfo = bookService.findBookByPage(name,cataId,now,size);
            result.setCode(200);
            result.setMsg("查询成功！");
            result.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @GetMapping("/getBookeByKey/{id}")
    @ResponseBody
    public ResponseResult<BookVo> getBookeByKey(@PathVariable("id") int id){
        ResponseResult<BookVo> result = new ResponseResult<>();
        try {
            BookVo book = bookService.findBookByKey(id);
            result.setCode(200);
            result.setMsg("查询成功！");
            result.setData(book);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }


}
